package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        //从当前线程中获取user_id
        Long userId = BaseContext.getCurrentId();

        //select * from shopping_cart where user_id=?
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .orderByDesc(ShoppingCart::getCreateTime)
                .list();
        return R.success(list);
    }

    /**
     * 添加购物车
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart cart) {

        //从当前线程中获取user_id
        Long userId = BaseContext.getCurrentId();

        //1. 判断购物车是否有该商品    select * from shopping_cart where user_id=? and name=?
        //1. 判断购物车是否有该商品    select * from shopping_cart where user_id=? and ( dish_id=? | setmeal_id=? )
        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(cart.getDishId() != null, ShoppingCart::getDishId, cart.getDishId())
                .eq(cart.getSetmealId() != null, ShoppingCart::getSetmealId, cart.getSetmealId())
                .eq(ShoppingCart::getUserId, userId)
                .one();

        if (one == null) {
            //说明没有，需要添加操作
            cart.setNumber(1);
            cart.setUserId(userId);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(cart); //TODO 传入的是cart
            return R.success(cart);
        } else {
            //说明有，需要更新操作
            one.setNumber(one.getNumber() + 1);
            one.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(one); //TODO 传入的是one
            return R.success(one);
        }

    }

    /**
     * 减购物项
     */
    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart cart) {

        //从当前线程中获取user_id
        Long userId = BaseContext.getCurrentId();

        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(cart.getDishId() != null, ShoppingCart::getDishId, cart.getDishId())
                .eq(cart.getSetmealId() != null, ShoppingCart::getSetmealId, cart.getSetmealId())
                .eq(ShoppingCart::getUserId, userId)
                .one();

        if (one.getNumber() == 1) {
            shoppingCartService.removeById(one.getId());
            one.setNumber(0); //配置前端展示
        } else {
            one.setNumber(one.getNumber() - 1);
            one.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    public R<String> clean() {

        //从当前线程中获取user_id
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId, userId)
                .remove();
        return R.success("清空成功");
    }

}
